package com.ntt.benew.dictionary.utils

import android.graphics.drawable.GradientDrawable
import android.view.View
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.SizeUtils

/**
 * Created by Android Studio.
 * User: tw
 * Date: 2023/11/1
 * Time: 15:59
 */

/**
 * 获取圆角背景
 * @param radii 四个角弧度
 * @param color 颜色
 */
fun View.setGradientDrawable(color: Int, radii: FloatArray) {
    background = GradientDrawable().apply {
        setColor(ContextCompat.getColor(context, color))
        cornerRadii = radii
    }
}

/**
 * 获取圆角背景
 * @param radiusDb 弧度
 * @param color 颜色
 */
fun View.setGradientDrawable(radiusDb: Int, color: Int) {
    background = GradientDrawable().apply {
        cornerRadius =
            SizeUtils.dp2px(radiusDb.toFloat()).toFloat()
        setColor(color)
    }
}


/**
 * 获取全圆角背景
 * @param color 颜色
 */
fun View.set360GradientDrawable(color: Int) {
    background = GradientDrawable().apply {
        cornerRadius = 360f
        setColor(color)
    }
}

/**
 * 获取只有边框的圆角背景
 * @param radiusDb 弧度
 * @param color 颜色
 * @param width 边框宽度
 * @param bgColor 背景颜色
 */
fun View.setGradientDrawableOnlyStroke(radiusDb: Int, color: Int, width: Int, bgColor: Int? = null) {
    background = GradientDrawable().apply {
        cornerRadius = radiusDb.toFloat()
        setStroke(SizeUtils.dp2px(width.toFloat()), color)
        bgColor?.let { setColor(it) }
    }
}

/**
 * 设置背景渐变、方向
 *
 * @param colorArray
 * @param radiusDp
 * @param iOrientation
 * @return
 */
fun View.setDrawableBackground(
    colorArray: IntArray,
    radiusDp: Float,
    iOrientation: GradientDrawable.Orientation = GradientDrawable.Orientation.LEFT_RIGHT
) {
    background = GradientDrawable().apply {
        cornerRadius = radiusDp
        colors = colorArray
        orientation = iOrientation
        gradientType = GradientDrawable.LINEAR_GRADIENT
    }
}

